package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.Method;

public class Text6 {
    public static void main(String[] args) throws Exception {
        File file = new File(
                Text6.class.getResource(".").toURI()
        );
        String packageName = Text6.class.getPackage().getName();
        File[] subs = file.listFiles(f -> f.getName().endsWith(".class"));
        for (File sub : subs
        ) {
            String fileName = sub.getName();
            String className = fileName.substring(0, fileName.indexOf("."));
            Class cls = Class.forName(packageName + "." + className);
            if (cls.isAnnotationPresent(AutoRunClass.class)) {
                Object obj = cls.newInstance();
                Method[] methods = cls.getDeclaredMethods();
                for (Method method : methods
                ) {
                    if (method.isAnnotationPresent(AutoRunMethod.class)) {
                        AutoRunMethod art = method.getAnnotation(AutoRunMethod.class);
                        int arts = art.value();
                        for (int i = 0; i < arts; i++) {
                            System.out.print((i + 1) + "次");
                            method.invoke(obj);
                        }
                    }
                }
            }
        }
    }
}
